﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace StringExtensions
{
  public static class ToDictionaryConverter
  {
    public static IDictionary<string, string> ToDictionary1(this string s)
    {
      return s
        .Split(';')
        .Select(item => item.Split('='))
        .ToDictionary(itemsSplittedByEqualSign => itemsSplittedByEqualSign[0], itemsSplittedByEqualSign => itemsSplittedByEqualSign[1]);
    }

    public static IDictionary<string, string> ToDictionary(this string s)
    {
      var itemsSplittedBySemicolon = s.Split(';');
      var dictionary = new Dictionary<string, string>();

      foreach (var item in itemsSplittedBySemicolon)
      {
        var itemsSplittedByEqualSign = item.Split('=');

        if (itemsSplittedByEqualSign.Length != 2)
          throw new Exception(string.Format("Number of items not 2 (current:{0})", itemsSplittedByEqualSign.Length));

        dictionary.Add(itemsSplittedByEqualSign[0], itemsSplittedByEqualSign[1]);
      }

      return dictionary;
    }

    public static IDictionary<string, string> ToDictionary3(this string s)
    {
      var itemsSplittedBySemicolon = s.Split(';');
      var dictionary = new Dictionary<string, string>();

      foreach (var item in itemsSplittedBySemicolon)
      {
        var itemsSplittedByEqualSign = item.Split('=');

        //if (itemsSplittedByEqualSign.Length != 2)
        //  throw new Exception(string.Format("Number of items not 2 (current:{0}", itemsSplittedByEqualSign.Length));

        dictionary.Add(itemsSplittedByEqualSign[0], itemsSplittedByEqualSign[1]);
      }

      return dictionary;
    }
  }
}
